#ifndef VEC_IMPL_H_
#define VEC_IMPL_H_
// 配置默认最小容量为8
#ifndef CTL_VEC_MINIMAL_SIZE
#define CTL_VEC_MINIMAL_SIZE 8
#endif

// 配置默认扩容因子为2倍
#ifndef CTL_VEC_GROW_FACTOR
#define CTL_VEC_GROW_FACTOR 2
#endif

// 实现vector函数（带修饰符版本）
#define IMPL_QUAL_VEC(TYPE, QUAL)                                                                       \
    /* 获取当前元素数量 */                                                                              \
    QUAL size_t ctl_vec_##TYPE##_size(ctl_vec_##TYPE *vec) {                                            \
        return vec->size;                                                                               \
    }                                                                                                   \
    /* 获取起始索引 */                                                                                  \
    QUAL size_t ctl_vec_##TYPE##_begin_idx(ctl_vec_##TYPE *vec) {                                       \
        return vec->begin_idx;                                                                          \
    }                                                                                                   \
    /* 带初始大小的构造函数 */                                                                          \
    QUAL int ctl_vec_##TYPE##_ctor_by_size(ctl_vec_##TYPE *vec, size_t size) {                          \
        vec->begin_idx = 0;                                                                             \
        vec->size = size;                                                                               \
        vec->capacity = (size < CTL_VEC_MINIMAL_SIZE) ? CTL_VEC_MINIMAL_SIZE : size;                    \
        vec->data = (TYPE *)malloc(vec->capacity * sizeof(TYPE));                                       \
        if (vec->data == NULL) return EOOM;                                                             \
        for (size_t idx = 0u; idx < vec->capacity; idx++) {                                             \
            if (TYPE##_ctor(vec->data + idx) != 0) {                                                    \
                for (size_t j = 0; j < idx; j++) TYPE##_dtor(vec->data + j);                            \
                free(vec->data);                                                                        \
                vec->data = NULL;                                                                       \
                return EFAIL;                                                                           \
            }                                                                                           \
        }                                                                                               \
        return EOK;                                                                                     \
    }                                                                                                   \
    /* 默认构造函数（使用最小容量）*/                                                                   \
    QUAL int ctl_vec_##TYPE##_ctor(ctl_vec_##TYPE *vec) {                                               \
        return ctl_vec_##TYPE##_ctor_by_size(vec, 0);                                \
    }                                                                                                   \
    /* 析构函数 */                                                                                      \
    QUAL void ctl_vec_##TYPE##_dtor(ctl_vec_##TYPE *vec) {                                              \
        if (vec->data) {                                                                                \
            for (size_t idx = 0; idx < vec->capacity; idx++) {                                          \
                TYPE##_dtor(vec->data + idx);                                                           \
            }                                                                                           \
            free(vec->data);                                                                            \
            vec->data = NULL;                                                                           \
        }                                                                                               \
        vec->size = 0;                                                                                  \
        vec->capacity = 0;                                                                              \
        vec->begin_idx = 0;                                                                             \
    }                                                                                                   \
    /* 扩容实现 */                                                                                      \
    QUAL int ctl_vec_##TYPE##_reserve(ctl_vec_##TYPE *vec, size_t new_capacity) {                       \
        if (new_capacity == 0) return EINDEX;                                                           \
        if (new_capacity <= vec->capacity) return EOK;                                                  \
        TYPE *new_data = (TYPE *)malloc(new_capacity * sizeof(TYPE));                                   \
        if (new_data == NULL) return EOOM;                                                              \
        for (size_t idx = 0u; idx < new_capacity; idx++) {                                              \
            if (TYPE##_ctor(new_data + idx) != 0) {                                                     \
                for (size_t j = 0; j < idx; j++) TYPE##_dtor(new_data + j);                             \
                free(new_data);                                                                         \
                return EFAIL;                                                                           \
            }                                                                                           \
        }                                                                                               \
        /* 迁移数据 */                                                                                  \
        for (size_t idx = 0; idx < vec->size; idx++) {                                                  \
            if (TYPE##_copy(new_data + idx, vec->data + vec->begin_idx + idx) != 0) {                   \
                for (size_t j = 0; j < new_capacity; j++) TYPE##_dtor(new_data + j);                    \
                free(new_data);                                                                         \
                return EFAIL;                                                                           \
            }                                                                                           \
        }                                                                                               \
        /* 释放旧数据 */                                                                                \
        for (size_t idx = 0; idx < vec->capacity; idx++) {                                              \
            TYPE##_dtor(vec->data + idx);                                                               \
        }                                                                                               \
        free(vec->data);                                                                                \
        vec->data = new_data;                                                                           \
        vec->capacity = new_capacity;                                                                   \
        vec->begin_idx = 0;                                                                             \
        return EOK;                                                                                     \
    }                                                                                                   \
    QUAL int ctl_vec_##TYPE##_resize(ctl_vec_##TYPE *vec, size_t new_size) {                            \
        if (vec->begin_idx + new_size > vec->capacity) {                                                \
            int res = ctl_vec_##TYPE##_reserve(vec, vec->begin_idx + new_size);                         \
            if (res != EOK) return res;                                                                 \
        }                                                                                               \
        vec->size = new_size;                                                                           \
        return EOK;                                                                                     \
    }                                                                                                   \
    QUAL int ctl_vec_##TYPE##_copy(ctl_vec_##TYPE *dst, const ctl_vec_##TYPE *src) {                    \
        /* 如果目标 vector 未初始化，先初始化它 */                                                      \
        if (dst->data == NULL) {                                                                        \
            dst->begin_idx = 0;                                                                         \
            dst->size = src->size;                                                                      \
            dst->capacity = src->size;                                                                  \
            dst->data = (TYPE *)malloc(dst->capacity * sizeof(TYPE));                                   \
            if (dst->data == NULL) return EOOM;                                                         \
            for (size_t idx = 0u; idx < dst->capacity; idx++) {                                         \
                if (TYPE##_ctor(dst->data + idx) != 0) {                                                \
                    for (size_t j = 0; j < idx; j++) TYPE##_dtor(dst->data + j);                        \
                    free(dst->data);                                                                    \
                    dst->data = NULL;                                                                   \
                    return EFAIL;                                                                       \
                }                                                                                       \
            }                                                                                           \
        }                                                                                               \
        /* 如果目标 vector 已初始化但容量不足，调整容量 */                                              \
        else if (dst->capacity < src->size) {                                                           \
            for (size_t idx = 0; idx < dst->capacity; idx++) {                                          \
                TYPE##_dtor(dst->data + idx);                                                           \
            }                                                                                           \
            free(dst->data);                                                                            \
            dst->capacity = src->size;                                                                  \
            dst->data = (TYPE *)malloc(dst->capacity * sizeof(TYPE));                                   \
            if (dst->data == NULL) return EOOM;                                                         \
            for (size_t idx = 0u; idx < dst->capacity; idx++) {                                         \
                if (TYPE##_ctor(dst->data + idx) != 0) {                                                \
                    for (size_t j = 0; j < idx; j++) TYPE##_dtor(dst->data + j);                        \
                    free(dst->data);                                                                    \
                    dst->data = NULL;                                                                   \
                    return EFAIL;                                                                       \
                }                                                                                       \
            }                                                                                           \
        }                                                                                               \
        dst->begin_idx = 0;                                                                             \
        dst->size = src->size;                                                                          \
        /* 拷贝数据 */                                                                                 \
        for (size_t idx = 0; idx < src->size; idx++) {                                                  \
            if (TYPE##_copy(dst->data + idx, src->data + src->begin_idx + idx) != 0) {                  \
                return EFAIL;                                                                           \
            }                                                                                           \
        }                                                                                               \
        return EOK;                                                                                     \
    }                                                                                                   \
    QUAL int ctl_vec_##TYPE##_get_at(ctl_vec_##TYPE *vec, size_t index, TYPE *data) {                   \
        if (index >= vec->size) return EINDEX;                                                          \
        return TYPE##_copy(data, vec->data + vec->begin_idx + index);                                   \
    }                                                                                                   \
    QUAL int ctl_vec_##TYPE##_set_at(ctl_vec_##TYPE *vec, size_t index, const TYPE value) {             \
        if (index >= vec->size) return EINDEX;                                                          \
        return TYPE##_copy(vec->data + vec->begin_idx + index, &value);                                 \
    }                                                                                                   \
    QUAL int ctl_vec_##TYPE##_push_back(ctl_vec_##TYPE *vec, const TYPE data) {                         \
        if (vec->begin_idx + vec->size >= vec->capacity) {                                              \
            size_t new_capacity = vec->capacity * CTL_VEC_GROW_FACTOR;                                 \
            int res = ctl_vec_##TYPE##_reserve(vec, new_capacity);                                     \
            if (res != EOK) return res;                                                                 \
        }                                                                                               \
        int res = TYPE##_copy(vec->data + vec->begin_idx + vec->size, &data);                           \
        if (res == EOK) vec->size++;                                                                    \
        return res;                                                                                     \
    }                                                                                                   \
    QUAL int ctl_vec_##TYPE##_pop_back(ctl_vec_##TYPE *vec, TYPE *data) {                               \
        if (vec->size == 0) return EEMPTY;                                                              \
        int res = TYPE##_copy(data, vec->data + vec->begin_idx + vec->size - 1u);                       \
        if (res == EOK) vec->size--;                                                                    \
        return res;                                                                                     \
    }

#endif

#define IMPL_VEC(TYPE) IMPL_QUAL_VEC(TYPE, )
#define IMPL_STATIC_VEC(TYPE) IMPL_QUAL_VEC(TYPE, static)
#define IMPL_INLINE_STATIC_VEC(TYPE) IMPL_QUAL_VEC(TYPE, inline static)